/*
* © 2010 Autodata Solutions Company. All Rights Reserved
* This source code is the confidential and proprietary information of Autodata Solutions Company.
* The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
* without the prior written consent of Autodata Solutions Company.
*/
Ext.ns('net.autodata.window');

(function() {
    /**
    * Postal code window private widget.
    * @class PostalCodeWindow
    * @extends Ext.Window
    * @namespace net.autodata.window
    */
    var Window = Ext.extend(Ext.Window, {
        postalcode: undefined,
        width: 245,
        cls: 'a-window',
        modal: true,
        resizable: false,

        constructor: function(cfg) {
            cfg = cfg || {};

            this.addEvents({
                /**
                * @event submit
                * Fires when this submit button receives is clicked.
                * @param {Ext.Window} this
                * @param {String} value The postal code text value
                */
                'submit': true
            });

            Window.superclass.constructor.call(this, cfg);
        },

        /**
        * Toolbar's on render event handler.
        */
        initComponent: function() {
            if (!this.title) {
                this.title = ASC.message('ca.autotrader.compare.postalcode.window.label.title');
            }
            
            Window.superclass.initComponent.apply(this);
            this.add([
                        {
                            xtype: 'box',
                            html: ASC.message('ca.autotrader.compare.postalcode.window.label.description').replace('{value}')
                        },
                        {
                            xtype: 'box',
                            itemId: 'error',
                            hidden: true,
                            cls: 'message-error',
                            style: {
                                padding: '16px 0 0 0'
                            },
                            html: ASC.message('ca.autotrader.compare.postalcode.window.error.invalidformat')
                        },
                        {
                            xtype: 'panel',
                            itemId: 'pnlOperation',
                            autoHeight: true,
                            border: false,
                            style: {
                                padding: '16px 0 20px 0'
                            },
                            layout: {
                                type: 'hbox',
                                padding: '0 0 2px 0'
                            },
                            items: [{
                                    xtype: 'postalcode',
                                    itemId: 'txtPostalCode',
                                    listeners: {
                                        'valid': this.onValid,
                                        scope: this
                                    },
                                    required: true,
                                    text: (this.postalcode !== undefined || this.postalcode !== null ? this.postalcode : '')
                                },
                                {
                                    xtype: 'button',
                                    text: ASC.message('net.autodata.label.submit'),
                                    style: {
                                        marginLeft: '20px'
                                    },
                                    listeners: {
                                        'click': this.onSubmitClick.createDelegate(this, null, 0)
                                    }
                            }]
                        },
                        {
                            xtype: 'panel',
                            border: false,
                            items: [{
                                xtype: 'button',
                                handleMouseEvents: false,
                                ctCls: 'a-btn-plain',
                                text: ASC.message('ca.autotrader.compare.postalcode.window.label.link'),
                                listeners: {
                                    'click': this.onProceedEmptyClick.createDelegate(this, null, 0)
                                }
                            }]
                        }
                ]);
        },

        //Event Handlers

        /**
        * Triggers the error message to be hidden
        * @method onValid
        * @param {Ext.Field} field
        * @private
        */
        onValid: function(field) {
            var error = this.getComponent('error');
            if (error) {
                error.hide();
            }
        },

        /**
        * Handles the submit button click
        * @method onSubmitClick
        * @private
        */
        onSubmitClick: function() {
            var panel = this.getComponent('pnlOperation');
            var error = this.getComponent('error');

            if (panel && error) {
                var textfield = panel.getComponent('txtPostalCode');
                if (textfield !== undefined) {
                    if (!textfield.isValid(true)) {
                        error.show();
                        return;
                    }

                    this.fireEvent('submit', this, textfield.getValue());
                }
            }

            this.close();
        },

        /**
        * Handles the proceed empty hyperlink click
        */
        onProceedEmptyClick: function() {
            this.fireEvent('submit', this, undefined);
            this.hide();
        },

        /**
        * Windows on destroy event handler.
        */
        onDestroy: function() {
            this.removeAll(true);
            Window.superclass.onDestroy.apply(this, arguments);
        }
    });

    net.autodata.window.PostalCodeWindow = Window;	
})();